package com.iad.controller.service
{
  import com.adobe.cairngorm.business.Responder;
  import com.iad.controller.event.*;
  import com.iad.model.*;
  import com.iad.model.valueobject.*;
  
  import flash.events.EventDispatcher;
  
  import mx.collections.ArrayCollection;
  import mx.controls.Alert;
  import mx.messaging.Channel;
  import mx.messaging.ChannelSet;
  import mx.messaging.channels.AMFChannel;
  import mx.rpc.AsyncToken;
  import mx.rpc.events.FaultEvent;
  import mx.rpc.events.ResultEvent;
  import mx.rpc.remoting.RemoteObject;
  import flash.net.navigateToURL;
  import flash.net.URLRequest;
  
  public class SiteService extends EventDispatcher{
    private var responder:Responder;
    private var service:RemoteObject;
    [Bindable] private var model:ModelLocatorImpl = ModelLocatorImpl.getInstance();
    
    public static const SITE_SERVICE:String = "siteService";
    
    public function SiteService() {    
      // initialize channel and remote object  
      var channelSet:ChannelSet = new ChannelSet();
      var myAMFChannel:Channel = new AMFChannel("iad-amf", "http://"+model.SERVER+":"+model.PORT+"/" + model.CONTEXT + "/messagebroker/amf");
      channelSet.addChannel(myAMFChannel);
      // here we still rely on remoting-config.xml (as pointed to by services-config.xml), specifying
      // the Java adapter and destination remote classes.  Perhaps we can find a way to define all of 
      // that here. 
    
      this.service = new RemoteObject();
      this.service.destination = SITE_SERVICE;
      this.service.channelSet = channelSet;
      this.responder = responder;
    }
    
    
    public function doLogin(login:String, password:String):void {
      var token:AsyncToken = service.doLogin(login, password);
      var responder:mx.rpc.Responder = new mx.rpc.Responder(doLoginOnResult, doLoginOnFault);
      token.addResponder(responder);
    }
    
    protected function doLoginOnResult(event:ResultEvent):void {
      if (event.result != null) {
        trace('SiteService::doLogin(): ' + (event.result as Site).pathLabel);
      }
      else {
        trace('SiteService::doLogin(): null result');
      }
      dispatchEvent(new ServiceResultEvent(ServiceResultEvent.DO_LOGIN_RESULT, event.result));
    }
        
    protected function doLoginOnFault(event:FaultEvent):void {
      trace(event.message as String);
    }
    
    public function getUser(id:Number):void {
      var token:AsyncToken = service.getUser(id);
      var responder:mx.rpc.Responder = new mx.rpc.Responder(getUserOnResult, getUserOnFault);
      token.addResponder(responder);
    }
    
    protected function getUserOnResult(event:ResultEvent):void {
      dispatchEvent(new ServiceResultEvent(ServiceResultEvent.GET_USER_RESULT, event.result));
    }
        
    protected function getUserOnFault(event:FaultEvent):void {
    }
    
    public function updateSiteProfile(id:Number, obj:Object):void {
      var token:AsyncToken = service.updateSiteProfile(id, obj);
      var responder:mx.rpc.Responder = new mx.rpc.Responder(updateSiteProfileOnResult, updateSiteProfileOnFault);
      token.addResponder(responder);
    }
    
    protected function updateSiteProfileOnResult(event:ResultEvent):void {
      dispatchEvent(new ServiceResultEvent(ServiceResultEvent.SUBMIT_REVIEW_RESULT, event.result));
    }
    
    protected function updateSiteProfileOnFault(event:FaultEvent):void {
    }
    
    public function submitReview(review:Review):void {
      var token:AsyncToken = service.submitReview(review);
      var responder:mx.rpc.Responder = new mx.rpc.Responder(submitReviewOnResult, submitReviewOnFault);
      token.addResponder(responder);
    }
    
    protected function submitReviewOnResult(event:ResultEvent):void {
      dispatchEvent(new ServiceResultEvent(ServiceResultEvent.SUBMIT_REVIEW_RESULT, event.result));
    }
    
    protected function submitReviewOnFault(event:FaultEvent):void {
    }
    
    public function deleteSong(id:Number):void {
      var token:AsyncToken = service.deleteSong(id);
      var responder:mx.rpc.Responder = new mx.rpc.Responder(deleteSongOnResult, deleteSongOnFault);
      token.addResponder(responder);
    }
    
    protected function deleteSongOnResult(event:ResultEvent):void {
      dispatchEvent(new ServiceResultEvent(ServiceResultEvent.DELETE_SONG_RESULT, event.result));
    }
        
    protected function deleteSongOnFault(event:FaultEvent):void {
    Alert.show("BLAH" + event);
    }
    
    
    public function getSite(siteId:Number):void {
      var token:AsyncToken = service.getSite(siteId);
      var responder:mx.rpc.Responder = new mx.rpc.Responder(getSiteOnResult, getSiteOnFault);
      token.addResponder(responder);
    }
    
    public function getSiteByLabel(siteLabel:String):void {
      var token:AsyncToken = service.getSiteByLabel(siteLabel);
      var responder:mx.rpc.Responder = new mx.rpc.Responder(getSiteOnResult, getSiteOnFault);
      token.addResponder(responder);
    }
    
    protected function getSiteOnResult(event:ResultEvent):void {
      dispatchEvent(new ServiceResultEvent(ServiceResultEvent.GET_SITE_RESULT, event.result));
    }
        
    protected function getSiteOnFault(event:FaultEvent):void {
      //Alert.show("BLAH" + event);
      var request:URLRequest = new URLRequest("http://"+model.SERVER+":"+model.PORT+"/"+ model.CONTEXT);
      navigateToURL(request, '_self');
    }
    
    public function getPlaylistFromNewSite(siteId:int):void {
      var token:AsyncToken = service.getPlaylistFromNewSite(siteId);
      var responder:mx.rpc.Responder = new mx.rpc.Responder(getPlaylistFromNewSiteOnResult, getPlaylistFromNewSiteOnFault);
      token.addResponder(responder);
    }
    
    protected function getPlaylistFromNewSiteOnResult(event:ResultEvent):void {
      dispatchEvent(new ServiceResultEvent(ServiceResultEvent.GET_PLAYLIST_FROM_NEW_SITE_RESULT, event.result));
    }
        
    protected function getPlaylistFromNewSiteOnFault(event:FaultEvent):void {
    Alert.show("BLAH" + event);
    }
    
    public function getSites():void {
      var token:AsyncToken = service.getSites();
      var responder:mx.rpc.Responder = new mx.rpc.Responder(getSitesOnResult, getSitesOnFault);
      token.addResponder(responder);
    }
    
    
    protected function getSitesOnResult(event:ResultEvent):void {
      dispatchEvent(new ServiceResultEvent(ServiceResultEvent.GET_SITES_RESULT, event.result));
    }
        
    protected function getSitesOnFault(event:FaultEvent):void {
    }
    
    public function getReviews(siteId:Number):void {
      var token:AsyncToken = service.getReviews(siteId);
      var responder:mx.rpc.Responder = new mx.rpc.Responder(getReviewsOnResult, getReviewsOnFault);
      token.addResponder(responder);
    }
    
    
    protected function getReviewsOnResult(event:ResultEvent):void {
      dispatchEvent(new ServiceResultEvent(ServiceResultEvent.GET_REVIEWS_RESULT, event.result));
    }
        
    protected function getReviewsOnFault(event:FaultEvent):void {
    }
    
    
    public function getNewReviews(siteId:Number):void {
      var token:AsyncToken = service.getNewReviews(siteId);
      var responder:mx.rpc.Responder = new mx.rpc.Responder(getNewReviewsOnResult, getNewReviewsOnFault);
      token.addResponder(responder);
    }
    
    
    protected function getNewReviewsOnResult(event:ResultEvent):void {
      dispatchEvent(new ServiceResultEvent(ServiceResultEvent.GET_NEW_REVIEWS_RESULT, event.result));
    }
        
    protected function getNewReviewsOnFault(event:FaultEvent):void {
    }
    
    
    public function approveNewReviews(siteId:Number):void {
      var rejectedReviewIds:ArrayCollection = new ArrayCollection();
      for each(var review:Review in model.currentSite.reviews) {
         if (review.approved == false) {
           trace('adding ' + review.id + ' to list of rejected reviews');
           rejectedReviewIds.addItem(review.id as Number);    
         } 
      } 
      var token:AsyncToken = service.approveNewReviews(siteId, rejectedReviewIds);
      var responder:mx.rpc.Responder = new mx.rpc.Responder(approveNewReviewsOnResult, approveNewReviewsOnFault);
      token.addResponder(responder);
    }
    
    
    protected function approveNewReviewsOnResult(event:ResultEvent):void {
      dispatchEvent(new ServiceResultEvent(ServiceResultEvent.APPROVE_NEW_REVIEWS_RESULT, event.result));
    }
        
    protected function approveNewReviewsOnFault(event:FaultEvent):void {
    }
    
    
    public function getSitePlaylists(siteId:Number):void {
      var token:AsyncToken = service.getSitePlaylists(siteId);
      var responder:mx.rpc.Responder = new mx.rpc.Responder(getSitePlaylistsOnResult, getSitePlaylistsOnFault);
      token.addResponder(responder);
    }
    
    
    protected function getSitePlaylistsOnResult(event:ResultEvent):void {
      dispatchEvent(new ServiceResultEvent(ServiceResultEvent.GET_SITE_PLAYLISTS_RESULT, event.result));
    }
        
    protected function getSitePlaylistsOnFault(event:FaultEvent):void {
    }
    
    
    public function getSiteVideos(siteId:Number):void {
      var token:AsyncToken = service.getSiteVideos(siteId);
      var responder:mx.rpc.Responder = new mx.rpc.Responder(getSiteVideosOnResult, getSiteVideosOnFault);
      token.addResponder(responder);
    }
    
    
    protected function getSiteVideosOnResult(event:ResultEvent):void {
      dispatchEvent(new ServiceResultEvent(ServiceResultEvent.GET_SITE_VIDEOS_RESULT, event.result));
    }
        
    protected function getSiteVideosOnFault(event:FaultEvent):void {
    }
    
    
    public function newSite(site:Site):void {
      var token:AsyncToken = service.newSite(site);
      var responder:mx.rpc.Responder = new mx.rpc.Responder(newSiteOnResult, newSiteOnFault);
      token.addResponder(responder);
    }
    
    protected function newSiteOnResult(event:ResultEvent):void {
      dispatchEvent(new ServiceResultEvent(ServiceResultEvent.NEW_SITE_RESULT, event.result));
    }
    
    protected function newSiteOnFault(event:FaultEvent):void {
    }
    
    
    public function savePage(page:Page):void {
      var token:AsyncToken = service.updatePage(page);
      var responder:mx.rpc.Responder = new mx.rpc.Responder(savePageOnResult, savePageOnFault);
      token.addResponder(responder);
    }
    
    
    public function savePages():void {
      var pages:ArrayCollection;
      if (model.app.currentState == model.MAIN_VIEW) {
        pages = model.mainPages;
      }
      else {
        pages = model.sitePages;
      }
      var token:AsyncToken = service.updatePages(pages);
      var responder:mx.rpc.Responder = new mx.rpc.Responder(savePagesOnResult, savePagesOnFault);
      token.addResponder(responder);
    }
    
    protected function savePagesOnResult(event:ResultEvent):void {
      dispatchEvent(new ServiceResultEvent(ServiceResultEvent.SAVE_PAGES_RESULT, event.result));
    }
    
    protected function savePagesOnFault(event:FaultEvent):void {
    }
    
    protected function savePageOnResult(event:ResultEvent):void {
      dispatchEvent(new ServiceResultEvent(ServiceResultEvent.SAVE_PAGE_RESULT, event.result));
    }
    
    protected function savePageOnFault(event:FaultEvent):void {
    }
    
    public function getPages(siteId:Number):void {
      var token:AsyncToken = service.getPages(siteId);
      var responder:mx.rpc.Responder;
      if (siteId != 0) {
        responder = new mx.rpc.Responder(getSitePagesOnResult, getPagesOnFault);
      }
      else {
        responder = new mx.rpc.Responder(getMainPagesOnResult, getPagesOnFault);
      }
      token.addResponder(responder);
    }
    
    protected function getSitePagesOnResult(event:ResultEvent):void {
      dispatchEvent(new ServiceResultEvent(ServiceResultEvent.GET_SITE_PAGES_RESULT, event.result));
    }
    
    protected function getMainPagesOnResult(event:ResultEvent):void {
      dispatchEvent(new ServiceResultEvent(ServiceResultEvent.GET_MAIN_PAGES_RESULT, event.result));
    }
    
    protected function getPagesOnFault(event:FaultEvent):void {
    }
    
    
    public function getSongsInPlaylist(playlistId:Number):void {
      var token:AsyncToken = service.getSongsInPlaylist(playlistId);
      var responder:mx.rpc.Responder = new mx.rpc.Responder(getSongsInPlaylistOnResult, getSongsInPlaylistOnFault);
      token.addResponder(responder);
    }
    
    protected function getSongsInPlaylistOnResult(event:ResultEvent):void {
      dispatchEvent(new ServiceResultEvent(ServiceResultEvent.GET_PLAYLIST_RESULT, event.result));
    }
        
    protected function getSongsInPlaylistOnFault(event:FaultEvent):void {
    Alert.show("BLAH" + event);
    }
    
  }
}